package edu.uoc.tdp.pac3.server;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.sql.Statement;
import javax.swing.JOptionPane;

import edu.uoc.tdp.pac3.beans.Area;
import edu.uoc.tdp.pac3.common.tools.TDSLanguageUtils;

/**
 * Classe per gestionar l'acces a la Taula d'espais
 * 
 * @author Eloi Rebollo i Francesc Cabrera
 * @version 1.0
 */
public class GestorEspais {
    
    private Connection objConn;
    private TDSLanguageUtils mess = null;
    
     public GestorEspais(GestorDisc gestor) {        
        objConn = gestor.getConnection();        
    }
     
    public Connection getConnection() {
        return objConn;
    }
    /**
     * Recerca d'un espai segons l'identificador
     * @param Identificador de l'espai
     * @return L'bjecte de la classe Espai amb identificador igual que el parametre
     */
    public Area getSpace(int idEspai) {
        Area space = null;
        
        String consulta = "SELECT area_id, area_name, creation_date "
                + "FROM tb_area WHERE area_id =  " + idEspai;
        try {
            Statement query = getConnection().createStatement();
            ResultSet rs = query.executeQuery(consulta);
            if (rs.next()) {
                space = new Area(rs.getInt(1), rs.getString(2), rs.getDate(3));
            }
            rs.close();
            query.close();

        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null,
                    mess.getMessage("gestor.space.err.db") + "\n" + e.getMessage(),
                    mess.getMessage("gestor.space"), JOptionPane.ERROR_MESSAGE);
        } catch (Exception eAl) {
            JOptionPane.showMessageDialog(null,
                    mess.getMessage("gestor.space.err.unknown") + "\n" + eAl.getMessage(),
                    mess.getMessage("gestor.space"), JOptionPane.ERROR_MESSAGE);
        }

        return space;
    }

    /**
     * Llista de tots els espais de la DB
     * @return Lista en tots els espais
     */
    public List<Area> getSpacesList() {

        List<Area> spaceList = new ArrayList<Area>();        
        try {
            String consulta = "SELECT area_id, area_name, creation_date " 
                            + "FROM tb_area order by area_name";

            Statement query = getConnection().createStatement();
            ResultSet rs = query.executeQuery(consulta);
            while (rs.next()) {
                Integer idArea = new Integer(rs.getInt("area_id"));                               
                spaceList.add( new Area(idArea, rs.getString("area_name"), rs.getDate("creation_date")));
            }
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null,
                    mess.getMessage("gestor.space.err.db") + "\n" + e.getMessage(),
                    mess.getMessage("gestor.space.err"), JOptionPane.ERROR_MESSAGE);
        } catch (Exception eAl) {
            JOptionPane.showMessageDialog(null,
                    mess.getMessage("gestor.space.err.unknown") + "\n" + eAl.getMessage(),
                    mess.getMessage("gestor.space.err"), JOptionPane.ERROR_MESSAGE);
        }
        
        return spaceList;
    }
}
